package com.rmuti.programming.ui;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.chula.testinterface.TestResultAdapter;
import com.rmuti.programming.editor.ProgrammingEditor;

public class ResultScore {
	private Display display;
	private Image bgImage;
	
	public ResultScore(Display display) {
		this.display = display;
		ImageRegistry registry = new ImageRegistry(display);
		registry.put("bg", ImageDescriptor.createFromFile(ResultScore.class, "../../../../icons/image/header.gif"));
		bgImage = registry.get("bg");
	}

	public void displayUI(TestResultAdapter testResult){
		final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
		shell.open();
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
		shell.setBounds(400, 120, 385, 390);
		shell.setBackground(display.getSystemColor(SWT.COLOR_GRAY));

		shell.addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				e.gc.fillRoundRectangle(10, 10, 90, 45, 25, 15);
			}
		});

		createHeaderMenu(display, shell);

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
		composite.setBounds(0, 100, 385, 170);

		Label name = new Label(composite, SWT.NONE);
		name.setText("ชื่อ เฉลิมวุฒิ น้อยอุ่นแสน");
		name.pack();
		
		Label score = new Label(composite, SWT.NONE);
		String resultText = String.format("คะแนนที่ได้ %2.2f เต็ม %2.2f",
				testResult.getPoint(), testResult.getFullPoint());
		score.setText(resultText);
		score.setBounds(40, 40, 250, 40);

		createButtomMenu(display, shell);

	}

	private  void createButtomMenu(final Display display,
			final Shell shell) {		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackgroundImage(bgImage);
		composite.setBounds(0, 335, 385, 55);

		Button backButton = new Button(composite, SWT.PUSH);
		backButton.setText("กลับ");
		backButton.setSize(50, 35);
		backButton.setLocation(110, 8);
		backButton.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				shell.close();
			}
		});

		Button button = new Button(composite, SWT.PUSH);
		button.setText("ส่งคะแนน");
		button.setSize(80, 35);
		button.setLocation(180, 8);
		button.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				shell.close();
				final UIView loginView = new LoginView(display);
				loginView.display();
			}
		});
		
	}

	private  void createHeaderMenu(final Display display,
			final Shell shell) {
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackgroundImage(bgImage);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBounds(0, 0, 385, 55);

		Label label1 = new Label(composite, SWT.NONE);
		label1.setText("ปฏิบัติการเขียนโปรแกรม");
		label1.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
		label1.setLocation(110, 12);
		label1.pack();
	}
}
